<?php

class Submissions extends Model {
	
	function Submissions()
	{
		parent::Model();
	}

	function get_latest_7()
	{
		$query = $this->db->query('SELECT `Subject` , `Title`, DATE_FORMAT(Time,"%W, %m.%e.%y @ %h:%i %p") as `TimeOfSubmission`, `ResourceIDNo` FROM `SubjectResource` ORDER BY `Time` DESC LIMIT 0 , 6');
		return $query;
	}

	function select_all()
	{
		$query = $this->db->query('SELECT `Subject` , `Title`, DATE_FORMAT(Time,"%W, %m.%e.%y @ %h:%i %p") as `TimeOfSubmission`, `ResourceIDNo` FROM `SubjectResource` ORDER BY `Time` DESC');
		return $query;
	}

	function authenticate_resource_id($array) {
		$this->db->where($array);
		$this->db->from('SubjectResource');
		$query = $this->db->get();
		return $query->num_rows();
	}

	function select_subject_resource($resource)
	{
		$this->db->where('ResourceIDNo', $resource);
		$this->db->from('SubjectResource');
		$this->db->select(array('Title', 'DATE_FORMAT(Time,"%W, %m.%e.%y @ %h:%i %p") as `TimeOfSubmission`', 'Type', 'Author', 'Username', 'Location', 'UploadIDNo'));
		$query = $this->db->get();
		return $query;
	}

	function select_all_subject_resources($subject)
	{
		$this->db->where('Subject', $subject);
		$this->db->from('SubjectResource');
		$query = $this->db->get();
		return $query;
	}

	function submit_resource_no_file($array)
	{
		$this->db->insert('SubjectResource', $array);
		return true;
	}

	function submit_resource($array1, $array2)
	{
		$this->db->trans_start();
		$this->db->insert('Upload', $array1);
		$blob_id = $this->db->insert_id();
		$ar = $array2;
		$ar['UploadIDNo'] = $blob_id;
		$this->db->insert('SubjectResource', $ar);
		$this->db->trans_complete();
		if ($this->db->trans_status() === FALSE)
		{ //error
			return false;
		}
		return true;
	}

	function get_blob($id)
	{
		$this->db->where('id', $id);
		$query = $this->db->get('Upload');
		return $query->row_array();
	}

	function delete($id)
	{
		$this->db->delete('SubjectResource', array('ResourceIDNo'=>$id));
		return TRUE;
	}
}

?>
